package com.lkl.demo.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 代码生成器
 * https://github.com/baomidou/generator
 */
public class CodeGenerator {

    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String userName = "root";
        String passWord = "root";

        //删除现有文件夹下内容
        String outputDirStr = System.getProperty("user.dir") + "/src/main/java";
        String singletonMapStr = System.getProperty("user.dir") + "/src/main/resources/mapper";
        String parentStr = "com.lkl.demo";

        FastAutoGenerator.create(url, userName, passWord)
                // 全局配置 文件作者名称
                .globalConfig((scanner, builder) ->
                        builder.author("likelong") //设置作者
                                .enableSwagger() //开启swagger
                                .commentDate((LocalDate.now()) + "") //注释日期
                                .fileOverride()//覆盖以生成文件
                                .dateType(DateType.ONLY_DATE) //时间策略 entity 类中使用Date	DateType.ONLY_DATE 默认值: DateType.TIME_PACK
                                .outputDir(outputDirStr) //指定输出目录
                )
                // 包配置
                .packageConfig((scanner, builder) ->
                        builder.parent(parentStr) //设置父包名
                                //  .moduleName("system") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, singletonMapStr)) // 设置mapperXml生成路径
                )
                // 策略配置
                .strategyConfig((scanner, builder) ->
                                builder.addInclude(getTables("t_goods,t_order,t_seckill_goods,t_seckill_order"))//设置要生成的表名
                                        .controllerBuilder()
                                        .enableRestStyle()
                                        .enableHyphenStyle()
                                        .entityBuilder()
                                        .enableLombok() //生成Lombok注解
//                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                        .build()
                                        .mapperBuilder()
                                        .enableBaseResultMap()
                                        .build()
                )
                //生成时不使用表前缀
                .strategyConfig((scanner, builder) -> builder.addTablePrefix("t_").build())
                //模版配置
                .templateConfig(
                        (scanner, builder) ->
                                builder.disable(TemplateType.ENTITY)
                                        .entity("/templates/vm/entity.java")
                                        .service("/templates/vm/service.java")
                                        .serviceImpl("/templates/vm/serviceImpl.java")
                                        .mapper("/templates/vm/mapper.java")
                                        .mapperXml("/templates/vm/mapper.xml")
                                        .controller("/templates/vm/controller.java")
                                        .build()
                )

                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .execute();
    }


    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

}
